<template>
    <div class="compassContent">
        <div class="compassBox">
            <van-field
                v-model="selectValue"
                is-link
                readonly
                label="地区"
                placeholder="选择区域"
                @click="showCityPicker = true"
            />
            <van-button class="btn" @keyup.enter="gasolineSelect()" @click="gasolineSelect()">
                查询
            </van-button>
        </div>
        <div class="compassResultBox">
            <div class="compassResultBoxItem" v-if="tableData.length > 0">
                <div class="compassResultBoxItemL">
                    <p class="title">{{ '92' }}</p>
                    <span style="margin-bottom: 20px;">{{ '型号' }}</span>
                    <p class="price">{{ `￥${tableData[0].p92 || '--'}/L` }}</p>
                    <span>{{ '价格' }}</span>
                </div>
                <div class="compassResultBoxItemR">
                    <SvgIcon width="80px" height="80px" name="fuelCan" color="#67dcfb"></SvgIcon>
                </div>
            </div>
            <div class="compassResultBoxItem" v-if="tableData.length > 0">
                <div class="compassResultBoxItemL">
                    <p class="title">{{ '95' }}</p>
                    <span style="margin-bottom: 20px;">{{ '型号' }}</span>
                    <p class="price">{{ `￥${tableData[0].p95 || '--'}/L` }}</p>
                    <span>{{ '价格' }}</span>
                </div>
                <div class="compassResultBoxItemR">
                    <SvgIcon width="80px" height="80px" name="fuelCan" color="#67dcfb"></SvgIcon>
                </div>
            </div>
            <div class="compassResultBoxItem" v-if="tableData.length > 0">
                <div class="compassResultBoxItemL">
                    <p class="title">{{ '98' }}</p>
                    <span style="margin-bottom: 20px;">{{ '型号' }}</span>
                    <p class="price">{{ `￥${tableData[0].p98 || '--'}/L` }}</p>
                    <span>{{ '价格' }}</span>
                </div>
                <div class="compassResultBoxItemR">
                    <SvgIcon width="80px" height="80px" name="fuelCan" color="#67dcfb"></SvgIcon>
                </div>
            </div>
        </div>
        <div class="compassdateBox" v-if="tableData.length > 0">
            <span>更新时间：{{ tableData[0].ct || '--' }}</span>
        </div>
        <van-popup v-model:show="showCityPicker" destroy-on-close round position="bottom">
            <van-picker
                title="选择要查询的省份"
                :columns="provinceList()"
                :columns-field-names="columnsFieldName"
                @change="onChange"
                @cancel="onCancel"
                @confirm="onConfirm"
            />
        </van-popup>
    </div>
</template>
    
<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import request from '@/utils/request'
import { provinceList } from '@/utils/plugins'
import { showToast, showFailToast } from 'vant'


const route = useRoute()
const router = useRouter()

const showCityPicker = ref<boolean>(false)
const selectValue = ref<string>('')
const tableData = ref<any>([])

const onCancel = () => {
    // 关闭城市选择器
    showCityPicker.value = false;
};

const columnsFieldName = {
    text: 'city',
    value: 'city'
}

const onChange = (item) => {
    console.log(item)
};

const onConfirm = ({selectedValues, selectedOptions}) => {
    // 确认选择
    showCityPicker.value = false;
    selectValue.value = selectedValues[0] || selectedOptions[0].value;
};

const reqGasolineInfo = (data: any) => request.post<any, any>(`https://route.showapi.com/138-46?showapi_appid=1783553&showapi_sign=40ebc3ff09fd4a08979bfc421db31f7d&data=0424&prov=${selectValue.value}`, data)
const gasolineSelect = () => {
    if (selectValue.value == '') {
        showToast('请选择查询区域')
        return
    }
    reqGasolineInfo({
        // showapi_appid: '1505398', //这里需要改成自己的appid
        // showapi_sign: '38a5f6fb1abe4355bc2f0d5435372138',  //这里需要改成自己的应用的密钥secret
        prov: selectValue.value,
        // data: '0424'
    }).then(res => {
        console.log('res', res)
        if (res.showapi_res_body.ret_code == 0) {
            tableData.value = res.showapi_res_body.list
            if (tableData.value.length == 0) {
                showToast('暂无数据')
            }
        } else {
            showFailToast(res.showapi_res_error || res.showapi_res_body.remark)
        }
    })
}

onMounted(() => {

})

</script>


<style lang="scss" scoped>
.compassContent {
    width: 100%;
    position: relative;
    padding: 15px;
    .compassBox {
        .btn {
            margin-top: 10px;
            width: 100%;
            height: 50px;
            border-radius: 16px;
            border: none;
            background-color: #67dcfb;
            font-size: 16px;
            font-weight: 500;
            color: var(--text-color);
        }
    }
    .compassResultBox {
        margin-top: 20px;
        width: 100%;
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        .compassResultBoxItem {
            margin-top: 20px;
            margin-right: 20px;
            // width: 40%;
            width: 145px;
            height: auto;
            aspect-ratio: 1 / 1;
            border-radius: 16px;
            background-color: #282828;
            overflow: hidden;
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            align-items: center;
            position: relative;
            .compassResultBoxItemL {
                height: 100%;
                display: flex;
                flex-direction: column;
                margin-left: 15px;
                color: #67dcfb;
                overflow: hidden;
                .title {
                    margin-top: 30px;
                    font-size: 28px;
                    font-weight: 500;
                }
                .price {
                    font-size: 14px;
                }
                span {
                    color: #666;
                    font-size: 12px;
                    white-space: pre-wrap;
                    overflow-wrap: break-word;
                }
            }
            .compassResultBoxItemR  {
                position: absolute;
                right: -20px;
            }
        }
    }
    .compassdateBox {
        margin-top: 30px;
        color: #666;
        font-size: 12px;
    }
}
</style>